home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 March / CHIP Turkiye Mart 1999.iso / araclar / Win98 / HTML-Tool / DWESD.EXE / data.z / mailform.txt < prev    next >
Encoding:
Text File  |  1998-04-02  |  7.6 KB  |  227 lines

  1. #!/usr/local/bin/perl
  2. # The line above tells the script where to find Perl. Ask
  3. # your system administrator what the path to Perl is on
  4. # your server and enter it on the line above. The bang (!)
  5. # is mandatory.
  6. #
  7. # mailform.cgi
  8. # http://www.macromedia.com/support/dreamweaver/
  9. # 08/30/97
  10. #
  11. # FUNCTION
  12. # -------------------
  13. # Sends the contents of an HTML form via e-mail. This mailer is
  14. # designed to print form fields that are named with a leading F
  15. # and a number (for example, F01_TextField, F02_SelectField, 
  16. # etc.) in order in the e-mail. Field names that do not begin 
  17. # with F, a number, and an underscore are also allowed, but they 
  18. # will not be printed in the body of the e-mail. This allows you 
  19. # to collect names and e-mail addresses for inclusion in the header 
  20. # of the mail without having to repeat them in the body. It also 
  21. # allows you to determine the order of the fields without having 
  22. # to put the field names in the script.
  23. #
  24. # CONFIGURATION
  25. # -------------------
  26. # The MAIL variable tells the script how to find the mailer on
  27. # your system. If you are not sure about the path to the sendmail 
  28. # program, ask your system administrator. The -t option allows 
  29. # you to specify the recipients in the content of the message.
  30.  
  31. $MAIL="/usr/lib/sendmail -t";
  32.  
  33. #
  34. #
  35. use CGI;
  36. $\="\n";
  37.  
  38. $req=new CGI;
  39. print $req->header;
  40.  
  41. # THE MAIN EVENT
  42. # -------------------
  43. %fields=&read_fields;
  44. &send_form;
  45. &print_thanks_page;
  46. exit(0);
  47.  
  48.  
  49. # SUBROUTINES
  50. # -------------------
  51.  
  52. sub read_fields{
  53.   my(%fields);
  54.   foreach $f ($req->param){
  55.     $name=&clean_name($f);
  56.     $fields{$f}{name}=$name;
  57.     $value=&clean_value($f);
  58.     $fields{$f}{value}=$value;
  59.   }
  60.   return(%fields);
  61. }
  62.  
  63. # The read_fields subroutine reads in each of the field names and
  64. # values from the form. It calls the clean_name and clean_value
  65. # subroutines, which remove the leading number and underscore from 
  66. # the name and concatenate multiple values, respectively.
  67.  
  68.  
  69. sub clean_name{
  70.   local($f)=shift;
  71.   $f=~s/^F\d+_//;
  72.   $f=~s/_/ /g;
  73.   return($f);
  74. }
  75.  
  76. sub clean_value{
  77.   local($f)=shift;
  78.   local(@val,$val);
  79.   @val=$req->param($f);
  80.   $#val-- unless $val[-1]=~/\S/;
  81.   $val=join(" - ",@val);
  82.   return($val);
  83. }
  84.  
  85.  
  86. sub send_form{
  87. #  return unless $fields{'mailto'}{'value'}=~/^[\w-]+@(yourdomain)\.com$/;
  88.  
  89. # If you would like to ensure that mail is not re-routed to 
  90. # another domain by a sneaky user who manipulates the 'mailto' 
  91. # field, replace the word 'yourdomain' in the line above with 
  92. # your actual domain. If your domain is a .net, .edu, or some 
  93. # suffix other than .com, you'll also need to replace that. When 
  94. # you've made the substitutions, uncomment the line by removing 
  95. # the pound sign (#).
  96.  
  97.   open(MAIL,"| $MAIL") or error("can't send mail");
  98.     
  99. # This is where the $MAIL variable you defined at the top of the 
  100. # script comes in. This line "pipes" the print MAIL commands to 
  101. # the sendmail program on your server.
  102.     
  103.   print MAIL "To: $fields{'mailto'}{'value'}";
  104.   print MAIL "From: $fields{'Name'}{'value'} <$fields{'E-mail'}{'value'}>";
  105.   print MAIL "Reply-To: $fields{'E-mail'}{'value'}";
  106.   print MAIL "Subject: $fields{'subject'}{'value'}";
  107.   print MAIL "";
  108.     
  109. # This part of the send_form routine is customizable. It 
  110. # currently relies on the fact that hidden form fields called 
  111. # 'mailto' and 'subject' have been included in the form to define 
  112. # the intended recipient(s) and the subject of the message, 
  113. # respectively. These fields could be called something else, as 
  114. # long as the names match in both the script and the form. The 
  115. # 'Name' and 'E-mail' fields are also included in the form, but 
  116. # their values are defined by the user. If you want to see how 
  117. # the form works as-is before making any changes, simply change 
  118. # the 'mailto' value to your own e-mail address in testform.html,
  119. # fill out the form, and submit it. 
  120.     
  121.   $\="";
  122.   $,=" - ";
  123.   
  124.   foreach $f (sort keys %fields){
  125.     next unless $f=~/^F\d/;
  126.     if ($fields{$f}{name} =~ /Comment/i) {
  127.       print MAIL "\n";
  128.       print MAIL "$fields{$f}{value}";
  129.       print MAIL "\n";
  130.     }
  131.     else{
  132.       print MAIL "$fields{$f}{name}: ";
  133.       print MAIL ($fields{$f}{value} ? $fields{$f}{value} : @{$fields{$f}{'values'}});
  134.       print MAIL "\n";
  135.     }
  136.   }
  137.     
  138. # This part of the send_form routine goes through each of the 
  139. # fields, one at a time. The second line of the foreach loop 
  140. # (line 125) says "skip to the next field unless the field name 
  141. # begins with F and a number." This is what prevents the 
  142. # unnumbered fields from printing. By the way, if you would like 
  143. # to use a field in the header *and* have it appear in the body 
  144. # of the message, simply assign it F and a number (for example, 
  145. # F02_Email). Just make sure that the field is referenced as 
  146. # F02_Email in the header section (lines 103-107). 
  147. #
  148. # Lines 126-130 say, "if the field name contains the word 
  149. #'Comment' (in upper case, lowercase, or mixed case), print a 
  150. # blank line, then the value of the field, and then another blank
  151. # line before moving on to the next field. This conditional is 
  152. # designed to set comments apart from other fields. If you'd like 
  153. # to test for something other than 'Comment,' or something in 
  154. # addition to 'Comment,' simply replace 'Comment' with the 
  155. # keyword you expect to appear in the field name or add " || 
  156. # /yourtext/i " (without the quotation marks) before the closing 
  157. # parentheses on line 126.
  158. #
  159. # Lines 131-135 execute when the field name does not include the 
  160. # word 'Comment.' They say, "First, print the field name and a 
  161. # colon. Then, if there's a single value, print that; otherwise, 
  162. # print the list of multiple values.
  163.     
  164.   $,="";
  165.   close(MAIL);
  166. }
  167.  
  168. # close(MAIL) wraps up the send_form function and sends the mail 
  169. # to the specified recipient (you, at least during the testing 
  170. # phase).
  171.  
  172.  
  173. sub print_thanks_page{
  174.   $\="";
  175.   print <<"EOM";
  176. <HTML>
  177. <HEAD>
  178. <TITLE>Thanks for your input</TITLE>
  179. </HEAD>
  180. <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
  181.  
  182. <h2>Thanks for your input!</h2>
  183.  
  184. Thanks for taking the time to fill out our form. If necessary, we will contact 
  185. you at the e-mail address you provided, $fields{'E-mail'}{'value'}.
  186.  
  187. </BODY>
  188. </HTML>
  189. EOM
  190. }
  191.  
  192. # Line 175 of the print_thanks_page subroutine says "print 
  193. # everything between this line and the line that starts with 
  194. # 'EOM'." It saves you from having to write a separate print 
  195. # statement for each line, and from having to escape any 
  196. # quotation marks that appear in the text to be printed (for
  197. # example, print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"000000\">";).
  198. #
  199. # This part of the code is also completely customizable; you can 
  200. # change any of the HTML, as well as include any form field names 
  201. # or values (in the code above, we've included the survey 
  202. # participant's e-mail address). For example, if you created a 
  203. # form that took an order for widgets, you could have your
  204. # print_thanks_page subroutine print out 
  205. #
  206. #   Thanks very much for your order. It will be mailed to the 
  207. #   following address within 7-10 days.
  208. #
  209. #   <pre>
  210. #   $fields{'F01_Name'}{'value'}
  211. #   $fields{'F02_Address1'}{'value'}
  212. #   $fields{'F03_Address2'}{'value'}
  213. #   $fields{'F04_City'}{'value'}, $fields{'F05_State'}{'value'}
  214. #   $fields{'F06_ZIP'}{'value'}
  215. #   </pre>
  216. #
  217. # (assuming that F01_Name, F02_Address1, F03_Address2, F04_City, 
  218. # F05_State, and F06_ZIP are all fields in your form). Feedback 
  219. # pages like this one let the users know that their orders were 
  220. # mailed off and let them check that the information they entered 
  221. # was correct. You might also include an e-mail address on this 
  222. # page to which the user can send any address corrections.
  223.  
  224.  
  225.  
  226.  
  227.